package com.kangye.enterprise.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kangye.enterprise.entity.Attention;
import com.kangye.enterprise.entity.Items;
import com.kangye.enterprise.entity.User;
import com.kangye.enterprise.entity.Users;
import com.kangye.enterprise.mapper.UserMapper;
import com.kangye.enterprise.service.UserService;
import com.kangye.enterprise.util.ViewJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: enterprise
 * @description: 实现用户逻辑接口
 * @author: 索腾龙
 * @create: 2021-05-26 10:16
 **/
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    //登陆
    @Override
    public User Login(String userUsername) {
        //返回
        return userMapper.Login(userUsername);
    }
    //分页查询
    @Override
    public ViewJsonObject<User> selUser(Integer page, Integer limit,User user) {
        //把当前页和每页显示的条数传进来
        PageHelper.startPage(page, limit);
        //查询用户
        List<User> list = userMapper.selUser(user);
        //创建分页对象,把数据存在pageInfo里
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        //返回json页面
        return new ViewJsonObject<User>(pageInfo.getList(), pageInfo.getTotal());
    }
    //添加用户
    @Override
    public Integer addUser(User user) {
        return userMapper.addUser(user);
    }
    //删除信息
    public ViewJsonObject<User> deleteUserById(Integer id) {
        //用Integer接收mapper接口
        Integer code = userMapper.deleteUserById(id);
        //判断是否大于0 如果大于0  删除成功,否则 删除失败
        if (code>0){
            return new ViewJsonObject<User>(0,"删除成功");
        }else {
            return new ViewJsonObject<User>(1,"删除失败");
        }
    }
    //修改用户信息
    @Override
    public Integer updUser(User user) {
        return userMapper.updUser(user);
    }
    //重置密码
    @Override
    public ViewJsonObject<User> updUserById(Integer id) {
        Integer code = userMapper.updUserById(id);
        if (code>0){
            return new ViewJsonObject<User>(0,"重置密码成功");
        }else {
            return new ViewJsonObject<User>(1,"重置密码失败");
        }
    }
    //分配角色
    @Override
    public Integer updUserByRoleId(Integer userRoleid, Integer id) {
        return userMapper.updUserByRoleId(userRoleid, id);
    }
    //批量删除
    @Override
    public ViewJsonObject<User> deleteUserByIds(List<String> idList) {
        Integer code = userMapper.deleteUserByIds(idList);
        if (code>0){
            return new ViewJsonObject<User>(0,"批量删除成功");
        }else {
            return new ViewJsonObject<User>(1,"批量删除失败");
        }
    }
    //修改密码
    @Override
    public Integer updPassword(User user) {
        return userMapper.updPassword(user);
    }
    //查看app用户信息
    @Override
    public ViewJsonObject<Users> selUsers(Integer page, Integer limit,String usersRolename) {
        //把当前页和每页显示的条数传进来
        PageHelper.startPage(page, limit);
        //查询用户
        List<Users> list = userMapper.selUsers(usersRolename);
        //创建分页对象,把数据存在pageInfo里
        PageInfo<Users> pageInfo = new PageInfo<Users>(list);
        //返回json页面
        return new ViewJsonObject<Users>(pageInfo.getList(), pageInfo.getTotal());
    }
    //查询用户申报多少条
    @Override
    public Integer selItemsCount(Integer id) {
        return userMapper.selItemsCount(id);
    }
    //查询用户关注多少条
    @Override
    public Integer selAttentionCount(Integer id) {
        return userMapper.selAttentionCount(id);
    }
    //查看用户申报的项目
    @Override
    public ViewJsonObject<Items> selItemsByIds(Integer page, Integer limit,Integer id) {
        //把当前页和每页显示的条数传进来
        PageHelper.startPage(page, limit);
        //查询用户
        List<Items> list = userMapper.selItemsByIds(id);
        //创建分页对象,把数据存在pageInfo里
        PageInfo<Items> pageInfo = new PageInfo<Items>(list);
        //返回json页面
        return new ViewJsonObject<Items>(pageInfo.getList(), pageInfo.getTotal());
    }
    //查询用户关注的信息
    @Override
    public ViewJsonObject<Attention> selAttentionByIds(Integer page, Integer limit, Integer id) {
        //把当前页和每页显示的条数传进来
        PageHelper.startPage(page, limit);
        //查询用户
        List<Attention> list = userMapper.selAttentionByIds(id);
        //创建分页对象,把数据存在pageInfo里
        PageInfo<Attention> pageInfo = new PageInfo<Attention>(list);
        //返回json页面
        return new ViewJsonObject<Attention>(pageInfo.getList(), pageInfo.getTotal());
    }
}